<?php
/**
 * @version			$Id$
 * @author			Richard Asp
 * @package			Joomla
 * @subpackage		gnudoor
 * @copyright		(C) 2010 gnudoor LLC
 * @license      GNU/GPL, see LICENSE.php
 * Joomla! is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 * See COPYRIGHT.php for copyright notices and details.
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.application.component.controller');

class GnudoorControllerGnudoor extends GnudoorController
{
	function __construct()
	{
	    parent::__construct();
	 
	    // Register Extra tasks
	    $this->registerTask( 'add'  ,     'edit' );
	}
    function edit() {
    	JRequest::setVar( 'view', 'gnudoor' );
		JRequest::setVar( 'layout', 'default');
        parent::display();
    }
    function viewrecord() {
        JRequest::setVar( 'view', 'gnudoor' );
		JRequest::setVar( 'layout', 'default_viewrecord');
        parent::display();
    }
    function newform() {
        JRequest::setVar( 'view', 'gnudoor' );
		JRequest::setVar( 'layout', 'newform');
        parent::display();
    }
    function tab_one() {
        JRequest::setVar( 'view', 'gnudoor' );
		JRequest::setVar( 'layout', 'default_step1');
        parent::display();
    }
    function tab_two() {
        JRequest::setVar( 'view', 'gnudoor' );
		JRequest::setVar( 'layout', 'default_step2');
        parent::display();
    }
    function tab_three() {
        JRequest::setVar( 'view', 'gnudoor' );
		JRequest::setVar( 'layout', 'default_step3');
        parent::display();
    }
    function tab_four() {
        JRequest::setVar( 'view', 'gnudoor' );
		JRequest::setVar( 'layout', 'default_step4');
        parent::display();
    }
    function tab_five() {
        JRequest::setVar( 'view', 'gnudoor' );
		JRequest::setVar( 'layout', 'default_step5');
        parent::display();
    }
    function save() {
        $model = $this->getModel('Gnudoor');
	if ($model->store($post)) {
		$msg = JText::_( 'Record Saved!' );
	} else {
		$msg = JText::_( 'Error Saving Record' );
	}
	// Check the table in so it can be edited.... we are done with it anyway
	$link = 'index.php?option=com_gnudoor&view=gnudoors';
	$this->setRedirect($link);
    }

    function saveselectprovider() {
        $model = $this->getModel('Gnudoor');
	if ($model->store($post)) {
		$msg = JText::_( 'Record Saved!' );
	} else {
		$msg = JText::_( 'Error Saving Record' );
	}
	// Check the table in so it can be edited.... we are done with it anyway
	$link = 'index.php?option=com_gnudoor&view=gnudoors';
	$this->setRedirect($link);
    }
    
    function cancel() {
        $msg = JText::_( 'Operation Cancelled' );
		$this->setRedirect( 'index.php?option=com_gnudoor&view=gnudoors');
    }
    function killrecord() {

    	$model = $this->getModel('Gnudoor');
	if ($model->store($post)) {
		$msg = JText::_( 'Record Killed!' );
	} else {
		$msg = JText::_( 'Error Saving Record' );
	}
        
        $this->setRedirect( 'index.php?option=com_gnudoor&view=gnudoors');
    }
    function zipCodeDistance($fromzip=null, $tozip=null) {
    	$fromziparray = $this->_getLatLongFromZip($fromzip);
    	$toziparray = $this->_getLatLongFromZip($tozip);
    	$dist = $this->_calc_distance($fromziparray, $toziparray );
    	return $dist;
    }

    function _calc_distance($point1, $point2) {
	    $radius      = 3958;      // Earth's radius (miles)
	    $pi          = 3.1415926;
	    $deg_per_rad = 57.29578;  // Number of degrees/radian (for conversion)
	
	    $distance = ($radius * $pi * sqrt(
	                ($point1['lat'] - $point2['lat'])
	                * ($point1['lat'] - $point2['lat'])
	                + cos($point1['lat'] / $deg_per_rad)  // Convert these to
	                * cos($point2['lat'] / $deg_per_rad)  // radians for cos()
	                * ($point1['long'] - $point2['long'])
	                * ($point1['long'] - $point2['long'])
	        ) / 180);
	
	    return $distance;  // Returned using the units used for $radius.
	}
	function _getLatLongFromZip($zipcode = null) {
		
		$xmlUrl = "http://ws.geonames.org/postalCodeSearch?country=US&postalcode=$zipcode"; // XML feed file/URL
		$xmlStr = file_get_contents($xmlUrl);
		$xmlObj = simplexml_load_string($xmlStr);
		$arrXml = $this->_objectsIntoArray($xmlObj);
		$data['lat'] = $arrXml['code']['lat'];
		$data['long'] = $arrXml['code']['lng'];
		return ($data);
	}

	function _objectsIntoArray($arrObjData, $arrSkipIndices = array())
	{
	    $arrData = array();
	    if (is_object($arrObjData)) {
	        $arrObjData = get_object_vars($arrObjData);
	    }
	   
	    if (is_array($arrObjData)) {
	        foreach ($arrObjData as $index => $value) {
	            if (is_object($value) || is_array($value)) {
	                $value = $this->_objectsIntoArray($value, $arrSkipIndices); // recursive call
	            }
	            if (in_array($index, $arrSkipIndices)) {
	                continue;
	            }
	            $arrData[$index] = $value;
	        }
	    }
	    return $arrData;
	}
	
}

?>